import { IConfig } from 'umi-types';
import defaultSettings from './defaultSettings'; // https://umijs.org/config/
import slash from 'slash2';
import { webpackPlugin } from './plugin.config';

const { pwa } = defaultSettings;

const plugins: (
  | [
      string,
      {
        antd: boolean;
        pwa: { workboxOptions: { importWorkboxFrom: string }; workboxPluginMode: string } | boolean;
        locale: { default: string; enable: boolean; baseNavigator: boolean };
        dva: { hmr: boolean };
      },
    ]
  | [
      string,
      { moveService: boolean; moveMock: boolean; modifyRequest: boolean; autoAddMenu: boolean },
    ]
)[] = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
        // default false
        enable: true,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
      },
      pwa: pwa
        ? {
            workboxPluginMode: 'InjectManifest',
            workboxOptions: {
              importWorkboxFrom: 'local',
            },
          }
        : false,
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
];

export default {
  plugins,
  hash: true,
  targets: {
    ie: 11,
  },

  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    //...darkTheme,
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      _: string,
      localName: string,
    ) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  history: 'hash',
  publicPath: '/', // 打包文件路径
  manifest: {
    basePath: '/',
  },
  copy: [
    {
      from: 'node_modules/@scene/scene-blocks/media',
      to: 'static/blocks-media',
    },
    {
      from: 'extension-worker.{js,js.map}',
      context: 'node_modules/@scene/scene-vm/dist/web',
    },
    {
      from: 'src/assets',
      to: 'static',
    },
  ],
  chainWebpack: webpackPlugin,
  proxy: {
    // "/api/msskill": {
    //   target: "http://192.168.204.104:8090/",
    //   changeOrigin: true,
    // },
    // "/api/mssoa": {
    //   target: "http://192.168.204.25:8189/",
    //   changeOrigin: true,
    // },
    // "/api": {
    //   target: "http://manage-ms.yapura-test.cloud.ci.iauto.com",
    //   changeOrigin: true,
    // },
  }
} as IConfig;
